package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.entity.MoneyFlows;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.LawyerRepository;
import com.lawyer.repository.MoneyFlowsRepository;
import com.lawyer.repository.UserRepository;

@RestController
@RequestMapping(value = "/system")
@Api(tags = "系统管理-统计")
@SuppressWarnings("all")
public class SystemStatisticsController {

	@Autowired
	private UserRepository userRepository;
	@Autowired
	private LawyerRepository lawyerRepository;
	@Autowired
	private EntityManager entityManager;
	
	@Autowired
	private MoneyFlowsRepository moneyFlowsRepository;

	@ApiOperation(httpMethod = "GET", value = "用户统计数据", notes = "registerCurrDayUser: 当天新增用户;register7DayUser: 最近7天新增用户;register30DayUser:最近30天新增用户; totalUserCount: 用户总量;")
	@RequestMapping(value = "/userStatistics", method = RequestMethod.GET)
	public HttpEntity<?> userStatistics() {
		Message msg = new Message();
		Map<String, Object> resutl = new HashMap<String, Object>();
		resutl.put("registerCurrDayUser", 0);
		resutl.put("register7DayUser", 0);
		resutl.put("register30DayUser", 0);
		resutl.put("totalUserCount", 0);
		
		Object registerCurrDayUser = userRepository.registerCurrDay();
		
		Object register7DayUser = userRepository.register7Day();
		
		Object register30DayUser = userRepository.register30Day();
		
		Object totalUserCount = userRepository.totalUserCount();
		
		
		if (registerCurrDayUser != null) {
			resutl.put("registerCurrDayUser", registerCurrDayUser);
		}
		if (register7DayUser != null) {
			resutl.put("register7DayUser", register7DayUser);
		}
		if (register30DayUser != null) {
			resutl.put("register30DayUser", register30DayUser);
		}
		if (totalUserCount != null) {
			resutl.put("totalUserCount", totalUserCount);
		}
		
		msg.setData(resutl);

		try {

		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
	
	

	@ApiOperation(httpMethod = "GET", value = "律师统计数据", notes = "registerCurrDayUser: 当天新增律师;register7DayUser: 最近7天新增律师;register30DayUser:最近30天新增律师; totalUserCount: 律师总量;")
	@RequestMapping(value = "/lawyerStatistics", method = RequestMethod.GET)
	public HttpEntity<?> lawyerStatistics() {
		Message msg = new Message();
		Map<String, Object> resutl = new HashMap<String, Object>();
		resutl.put("registerCurrDayUser", 0);
		resutl.put("register7DayUser", 0);
		resutl.put("register30DayUser", 0);
		resutl.put("totalLawyerCount", 0);
		
		Object registerCurrDayUser = lawyerRepository.registerCurrDay();
		
		Object register7DayUser = lawyerRepository.register7Day();
		
		Object register30DayUser = lawyerRepository.register30Day();
		
		Object totalLawyerCount = lawyerRepository.totalLawyerCount();
		
		if (registerCurrDayUser != null) {
			resutl.put("registerCurrDayUser", registerCurrDayUser);
		}
		if (register7DayUser != null) {
			resutl.put("register7DayUser", register7DayUser);
		}
		if (register30DayUser != null) {
			resutl.put("register30DayUser", register30DayUser);
		}
		if (totalLawyerCount != null) {
			resutl.put("totalLawyerCount", totalLawyerCount);
		}

		msg.setData(resutl);

		try {

		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
	
	
	@ApiOperation(httpMethod = "GET", value = "收益统计", notes = "收益统计")
	@RequestMapping(value = "/moneyFlowsStatistics", method = RequestMethod.GET)
	public HttpEntity<?> moneyFlowsStatistics() {
		Message msg = new Message();
		Map<String, Object> resutl = new HashMap<String, Object>();
		resutl.put("earningsCurrDay", 0);
		resutl.put("earnings7Day", 0);
		resutl.put("earnings30Day", 0);
		resutl.put("totalEarningsCount", 0);
		
		Object earningsCurrDay = moneyFlowsRepository.earningsCurrDay();
		
		Object earnings7Day = moneyFlowsRepository.earnings7Day();
		
		Object earnings30Day = moneyFlowsRepository.earnings30Day();
		
		Object totalEarningsCount = moneyFlowsRepository.totalEarningsCount();
		
		if (earningsCurrDay != null) {
			resutl.put("earningsCurrDay", earningsCurrDay);
		}

		if (earnings7Day != null) {
			resutl.put("earnings7Day", earnings7Day);
		}
		if (earnings30Day != null) {
			resutl.put("earnings30Day", earnings30Day);
		}
		if (totalEarningsCount != null) {
			resutl.put("totalEarningsCount", totalEarningsCount);
		}
		
		
		msg.setData(resutl);

		try {

		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(value = "通话记录列表", response = Page.class, responseContainer = "Page")
	@ApiImplicitParams({ @ApiImplicitParam(name = "page", required = false, value = "第几页", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "size", required = false, value = "分页条数", paramType = "query", dataType = "String") })
	@RequestMapping(value = "callRecords", method = RequestMethod.GET)
	public HttpEntity<?> callRecords(@PageableDefault(page = 0, size = 20, sort = { "createdDate" }, direction = Sort.Direction.DESC) Pageable pageable) {
		Message msg = new Message();
		try {
			StringBuffer sql = new StringBuffer("select * from money_flows where  description ='扣除咨询费' or description ='使用免费咨询券'  order by created_date desc");

			Query query = entityManager.createNativeQuery(sql.toString(), MoneyFlows.class);
			// 记录当前sql查询结果总条数
			List<MoneyFlows> counts = query.getResultList();
			// 设置分页参数
			query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
			query.setMaxResults(pageable.getPageSize());
			Page page = new PageImpl<MoneyFlows>(query.getResultList(), pageable, counts.size());

			return new ResponseEntity<>(page, HttpStatus.OK);

		} catch (Exception e) {
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
	

}
